// dictation-area.js - 听写区域组件
const app = getApp();

Component({
  data: {
    currentWordIndex: 0,
    totalWordsCount: 0,
    currentWord: null,
    currentMode: 'en_to_zh',
    userInput: '',
    pronunciationType: 'american',
    playbackSpeed: 1.0,
    isPlaying: false,
    answerAnimation: null,
    isAnswerCorrect: null,
    timeLimitEnabled: false,
    timeRemaining: 0,
    answeringMode: 'online'
  },
  lifetimes: {
    attached() {
      // 获取DictationManager实例
      this.dictationManager = app.getDictationManager();
      
      // 添加状态变化监听器
      this.stateChangeListener = this.onDictationStateChange.bind(this);
      this.dictationManager.addListener(this.stateChangeListener);
      
      // 初始化组件数据
      this.updateComponentData();
    },
    detached() {
      // 移除状态变化监听器
      if (this.stateChangeListener) {
        this.dictationManager.removeListener(this.stateChangeListener);
      }
    }
  },
  methods: {
    /**
     * 监听DictationManager状态变化
     */
    onDictationStateChange(state) {
      this.setData({
        currentWordIndex: state.currentWordIndex,
        totalWordsCount: state.totalWordsCount,
        currentWord: state.currentWord,
        currentMode: state.currentMode,
        userInput: state.userInput,
        pronunciationType: state.pronunciationType,
        playbackSpeed: state.playbackSpeed,
        isPlaying: state.isPlaying,
        answerAnimation: state.answerAnimation,
        isAnswerCorrect: state.isAnswerCorrect,
        timeLimitEnabled: state.timeLimitEnabled,
        timeRemaining: state.timeRemaining,
        answeringMode: state.answeringMode
      });
    },
    
    /**
     * 更新组件数据
     */
    updateComponentData() {
      const state = this.dictationManager.getState();
      this.setData({
        currentWordIndex: state.currentWordIndex,
        totalWordsCount: state.totalWordsCount,
        currentWord: state.currentWord,
        currentMode: state.currentMode,
        userInput: state.userInput,
        pronunciationType: state.pronunciationType,
        playbackSpeed: state.playbackSpeed,
        isPlaying: state.isPlaying,
        answerAnimation: state.answerAnimation,
        isAnswerCorrect: state.isAnswerCorrect,
        timeLimitEnabled: state.timeLimitEnabled,
        timeRemaining: state.timeRemaining,
        answeringMode: state.answeringMode
      });
    },
    
    /**
     * 结束听写
     */
    endDictation() {
      this.dictationManager.endDictation();
    },
    
    /**
     * 进入下一题（用于线下模式）
     */
    nextWord() {
      this.dictationManager.nextWord();
    },

    /**
     * 播放当前单词
     */
    playCurrentWord() {
      // 直接调用管理器的方法播放单词
      this.dictationManager.playCurrentWord();
    },

    /**
     * 处理用户输入
     */
    onInput(e) {
      const userInput = e.detail.value;
      // 直接更新管理器中的状态
      this.dictationManager.setState({
        userInput: userInput
      });
    },

    /**
     * 提交答案
     */
    submitAnswer() {
      const userInput = this.data.userInput;
      if (!userInput.trim()) {
        wx.showToast({
          title: '请输入答案',
          icon: 'none'
        });
        return;
      }
      // 直接调用管理器的方法提交答案
      this.dictationManager.submitAnswer(userInput);
    }
  }
});